﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RemoteMonster_AI : MonoBehaviour
{
    public Transform player; // 主角的Transform
    public float shootingRange = 10f; // 射程距离
    public float escapeRange = 5f; // 逃离距离
    public float moveSpeed = 3f; // 移动速度
    public GameObject bulletPrefab; // 子弹预制体

    private bool isEscaping = false;
    private Vector3 escapeDirection;

    private void Start()
    {
        // 获取主角的Transform
        player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    private void Update()
    {
        // 计算与主角的距离
        float distanceToPlayer = Vector3.Distance(transform.position, player.position);

        // 射击行为
        if (distanceToPlayer <= shootingRange)
        {
            Shoot();
        }

        // 逃离行为
        if (distanceToPlayer < escapeRange)
        {
            isEscaping = true;
            escapeDirection = transform.position - player.position;
            escapeDirection.Normalize();
        }
        else if (distanceToPlayer >= escapeRange * 2)
        {
            isEscaping = false;
        }

        // 移动行为
        if (isEscaping)
        {
            Debug.Log("移动");
            transform.position += escapeDirection * moveSpeed * Time.deltaTime;
        }
    }

    private void Shoot()
    {
        // 在怪物位置创建子弹
        Instantiate(bulletPrefab, transform.position, Quaternion.identity);
    }
}
